<template>
	<view :style="colorStyle">
		<!-- <view class="top-tabs">
			<view class="left">
				<view class="from">
					<yhdslplace @change="changeFrom">
						<view class="text">{{from}}</view>
					</yhdslplace>
					<image class="arrow-down" src="../../../static/images/arrow_down.png"></image>
				</view>
				<image class="direc" src="../../../static/images/direc_right.png"></image>
				<view class="to">
					<yhdslplace @change="changeTo">
						<view class="text">{{to}}</view>
					</yhdslplace>
					<image class="arrow-down" src="../../../static/images/arrow_down.png"></image>
				</view>
			</view>
		</view> -->
		<view class='return-list items' v-if="orderList.length">
			<view class='item' v-for="(item,index) in orderList" :key="index"
				@click='goOrderDetails(item)'>
				<view class="left">
					<view class="time">{{item.start_time|formatTime}}</view>
					<view class="date">{{item.start_time|formatDate}}</view>
				</view>
				<view class="right">
					<view class="section">
						<view class="name">【车主】{{item.name}}</view>
						<view class="num">【余座】{{item.has_location}}</view>
					</view>
					<view class="section">
						<view class="text">【车型】{{item.car_info}}</view>
					</view>
					<view class="section">
						<view class="text">【备注】{{item.remark}}</view>
					</view>
					<view class="footer">
						<view class="price">¥{{item.amount}}/座</view>
						<view class="sub">预订</view>
					</view>
				</view><!-- right end -->
			</view>
		</view>
		<view class='loadingicon acea-row row-center-wrapper' v-if="orderList.length > 0">
			<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>{{loadTitle}}
		</view>
		<view v-if="orderList.length == 0  && !loading">
			<emptyPage :title="$t(`暂无行程~`)"></emptyPage>
		</view>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<!-- #ifndef MP -->
		<home></home>
		<!-- #endif -->
	</view>
</template>

<script>
	import home from '@/components/home';
	import emptyPage from '@/components/emptyPage';
	import {travel_list} from '@/api/travel.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import colors from '@/mixins/color.js';
	import yhdslplace from '@/components/yhdsl-place/yhdsl-place.vue';
	
	export default {
		components: {
			home,
			yhdslplace,
			emptyPage,
			// #ifdef MP
			authorize
			// #endif
		},
		mixins: [colors],
		data() {
			return {
				type: 0,
				loading: false,
				loadend: false,
				loadTitle: this.$t(`加载更多`), //提示语
				orderList: [], //订单数组
				orderStatus: -3, //订单状态
				page: 1,
				limit: 10,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				from:'始发地',
				to:'目的地'
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getListData();
					}
				},
				deep: true
			}
		},
		mounted() {
			// console.info(options.type)
			// this.type = options.type;
			if (this.isLogin) {
				this.getListData();
			} else {
				toLogin();
			}
		},
		onLoad(options) {
			
		},
		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {
			this.page++;
			this.getListData();
		},
		onPullDownRefresh() {
			this.page = 1;
			this.getListData();
		},
		methods: {
			changeFrom(val) {
				console.log(val);
				this.from = val.data.join(',');
				this.page = 1;
				this.getOrderList();
			},
			changeTo(val) {
				console.log(val);
				this.to = val.data.join(',');
				this.page = 1;
				this.getOrderList(); 
			},
			onLoadFun() {
				this.getListData();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 去详情
			 */
			goOrderDetails: function(item) {
				let url = '/pages/main/query/detail?id=' + item.id;
				url += '&start_time='+item.start_time;
				url += '&start_city='+item.start_city;
				url += '&start_district='+item.start_district;
				url += '&end_city='+item.end_city;
				url += '&end_district='+item.end_district;
				url += '&car_info='+item.car_info;
				url += '&has_location='+item.has_location;
				url += '&amount='+item.amount;
				url += '&remark='+item.remark;
				uni.navigateTo({
					url: url
				})
			},
			/**
			 * 获取列表
			 */
			getListData(type) {
				let that = this;
				if (that.loading) return;
				if (that.page != 1 && that.loadend) return;
				uni.showLoading({
					title:'加载中...'
				});
				that.loading = true;
				that.loadTitle = "";
				travel_list({
					// type: that.orderStatus,
					page: that.page,
					limit: that.limit,
					from:that.from=="始发地"?'':that.from,
					to:that.to=="目的地"?'':that.to
				}).then(res => {
					uni.hideLoading();
					console.info(res)
					let list = res.data.list || [];
					let loadend = list.length < that.limit;
					that.loadend = loadend;
					that.loading = false;
					that.loadTitle = loadend ? that.$t(`我也是有底线的`) : that.$t(`加载更多`);
					if(that.page == 1){
						that.orderList = list;
					}else{ 
						if(list.length == 0){
							if(that.page > 1){
								that.page--;
							}else{
								that.page = 1;
							}
						}
						that.orderList = that.orderList.concat(list);
					}
					that.$set(that, 'orderList', that.orderList);
					
					
				}).catch(err => {
					uni.hideLoading();
					that.loading = false;
					that.loadTitle = that.$t(`加载更多`);
				});
			}
		},
		 //时间戳的处理    
		filters: {
			formatTime: function(value) {
				var date = new Date(value);
				var hours = date.getHours();
				if (hours < 10)
					hours = "0" + hours;
				var minutes = date.getMinutes();
				if (minutes < 10)
					minutes = "0" + minutes;
				var time = hours + ":" + minutes;
				return time;
			},
			formatDate: function(value) {
				var date = new Date(value);
				if(date.getDate() === new Date().getDate()){
					return '今天';
				}else if(date.getDate() === (new Date().getDate() + 1)){
					return '明天';
				}else{
					var month = date.getMonth() + 1;
					var d = date.getDate();
					if(d < 10){
						d = '0' + d;
					}
					return month + '月' + d + '日';
				}
			}
 
		},
	}
</script>

<style lang="scss" scoped>
	.items{
		display: flex;
		flex-direction: column;
		.item{
			background-color: white;
			display: flex;
			flex-direction: row;
			padding: 30rpx;
			margin-top: 13rpx;
			border-bottom: 1rpx solid #f1f1f1;
			.left{
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 100rpx;
				.time{
					font-size: 28rpx;
					font-weight: bold;
					color: black;
				}
				.date{
					font-size: 26rpx;
					color: gray;
					margin-top: 10rpx;
				}
			}
			.right{
				flex:1;
				margin-left: 30rpx;
				display: flex;
				flex-direction: column;
				.section{
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 30rpx;
					.name{
						font-size: 28rpx;
						color: black;
					}
					.num{
						font-size: 28rpx;
						color: black;
					}
				}
				.footer{
					display: flex;
					flex-direction: row;
					justify-content: flex-end;
					align-items: center;
					.price{
						font-size: 28rpx;
						color: black;
					}
					.sub{
						width: 100rpx;
						height: 60rpx;
						text-align: center;
						line-height: 60rpx;
						background-color: orange;
						color: white;
						margin-left: 30rpx;
					}
				}
			}//right end
		}
	}
	
	.top-tabs {
		display: flex;
		flex-direction: row;
		align-items: center;
		height: 80rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #f1f1f1;
		.left{
			display: flex;
			flex-direction: row;
			align-items: center;
			.from{
				display: flex;
				flex-direction: row;
				align-items: center;
				.text{
					font-size: 28rpx;
					color: black;
					width: 180rpx;
					text-align: right;
					margin-left: 20rpx;
					white-space: nowrap;
					text-overflow:ellipsis;
					overflow:hidden;
				}
				.arrow-down{
					width: 30rpx;
					height:30rpx;
					margin-left: 10rpx;
				}
			}
			.direc{
				margin-left: 80rpx;
				width: 30rpx;
				height:30rpx;
			}
			.to{
				margin-left: 80rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				.text{
					font-size: 28rpx;
					color: black;
					max-width: 200rpx;
					white-space: nowrap;
					text-overflow:ellipsis; 
					overflow:hidden;
				}
				.arrow-down{
					width: 30rpx;
					height:30rpx;
					margin-left: 10rpx;
				}
			}
		}
	}

	.top-tabs .tabs {
		position: relative;
		height: 100%;
		padding: 12px 0;
	}

	.btborder {
		&::after {
			position: absolute;
			content: ' ';
			width: 39px;
			height: 2px;
			background-color: var(--view-theme);
			bottom: 2px;
			left: 50%;
			margin-left: -19px;
		}
	}
</style>
